package ink.ykb.actDemo.util;

import org.slf4j.Logger;

import com.fasterxml.jackson.annotation.JsonIgnore;


/**
* ajax数据返回对象
*/
public class CommonResult {

	/** 服务器返回状态码：200 正常，用于提交成功 */
	@JsonIgnore
	private static final Integer STATUS_CODE_SUCCESS = 200;
	
	/**  服务器返回状态码：503 警告，用于数据校验失败  */
	@JsonIgnore
	private static final Integer STATUS_CODE_WARN = 503;
	
	/**  服务器返回状态码：500 异常，用于捕获异常  */
	@JsonIgnore
	private static final Integer STATUS_CODE_ERROR = 500;
    
	/**返回页面默认消息 : 操作成功 */
	@JsonIgnore
	private static final String SUCCESS = "操作成功";
	
	/**犯规页面默认消息 : 操作失败*/
	@JsonIgnore
	private static final String FAIL = "操作失败";
	
	/**异常消息 :  操作异常 */
	@JsonIgnore
	private static final String ERROR = "操作异常";
	
    private Integer code;
    private String message;
    private Object data;
    private Integer total;

    public static CommonResult build(Integer code, String message, Object Data,Integer total) {
        return new CommonResult(code, message, Data,total);
    }

    public static CommonResult resultSuccess(Object data,Integer total) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_SUCCESS;
    	result.message = SUCCESS;
    	result.data = data;
    	result.total=total;
    	return result;
    }

    public static CommonResult resultSuccess() {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_SUCCESS;
    	result.message = SUCCESS;
    	return result;
    }
    public static CommonResult resultSuccess(String message) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_SUCCESS;
    	result.message = message;
    	return result;
    }
    public static CommonResult resultFail(String message) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	result.message = message;
    	return result;
    }
    public static CommonResult resultFail(Exception e) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
		}else{
			result.message = FAIL;
		}
    	return result;
    }
    public static CommonResult resultFail(Exception e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
    		logger.error(e.getMessage());
    	}else{
    		result.message = FAIL;
    		logger.error(e.getMessage(), e);
    	}
    	return result;
    }
    public static CommonResult resultFail(Throwable e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
    		logger.error(e.getMessage());
    	}else{
    		result.message = FAIL;
    		logger.error(e.getMessage(), e);
    	}
    	return result;
    }
    public static CommonResult resultFail(Throwable e) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
    	}else{
    		result.message = FAIL;
    	}
    	return result;
    }
    
    public static CommonResult resultFail() {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_WARN;
    	result.message = FAIL;
    	return result;
    }

    public static CommonResult resultError(String message) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_ERROR;
    	result.message = message;
    	return result;
    }
    
    public static CommonResult resultError(Exception e) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_ERROR;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
		}else{
			result.message = ERROR;
		}
    	return result;
    }
    public static CommonResult resultError(Throwable e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_ERROR;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
    		logger.error(e.getMessage());
    	}else{
    		result.message = ERROR;
    		logger.error(e.getMessage(), e);
    	}
    	return result;
    }
    public static CommonResult resultError(Exception e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_ERROR;
    	if(e instanceof PlatformException || e instanceof PlatformRuntimeException){
    		result.message = e.getMessage();
    		logger.error(e.getMessage());
    	}else{
    		result.message = ERROR;
    		logger.error(e.getMessage(), e);
    	}
    	return result;
    }
    
    public static CommonResult resultError() {
    	CommonResult result = new CommonResult();
    	result.code = STATUS_CODE_ERROR;
    	result.message = ERROR;
    	return result;
    }
    
    public CommonResult() {
    		
    }

    public static CommonResult build(Integer code, String message,Integer total) {
        return new CommonResult(code, message, null,total);
    }

    public CommonResult(Integer code, String message, Object data,Integer total) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total=total;
    }



    public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}


}
